/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package controller;

import gui.Gui;
import gui.GuiInterface;
import ip.UrlLinks;
import ip.WhatIsMyIP;

import java.util.Timer;

import log.LogBuffer;
import log.LogInterface;
import config.Config;
import config.ConfigInterface;

/**
 * Initiates the controller of this application. This class handles
 * the communication between the different applications. 
 * @author Christian Masus
 *
 */
public class Controller implements ControllerInterface {

	private Timer timer;
	private String version;
	private UrlLinks urlLinks;
	private WhatIsMyIP whatIsMyIP;
	private ConfigInterface config;
	private LogInterface log;
	private GuiInterface gui;

	public Controller(String version, String configPath, String guiType) {

		this.timer = new Timer();
		this.version = version;
		this.urlLinks = new UrlLinks();
		this.whatIsMyIP = new WhatIsMyIP();
		this.config = new Config(configPath);
		setConfigFromArgs(guiType);
		this.log = new LogBuffer(config, timer);
		this.gui = new Gui((ControllerInterface)this);
	}

	/**
	 * Sets configuration values as given in the command line. This function
	 * will be extended as more command line args are implemented.
	 * @param guiType name of the GUI type to be used {"JavaGui", "JavaGuiNative", "Debug", "Text", "WebGui", ...}
	 */
	private void setConfigFromArgs(String guiType) {
		if(guiType != null && !guiType.equals("")) {
			config.setVariable("", "GuiType", guiType);
		}
	}

	/**
	 * Should be called when to terminate the SyncIT application.
	 * Closes all opened buffers and terminates all running threads.
	 */
	public void terminate() {
		timer.cancel();
	}



	public Timer getTimer() {
		return timer;
	}

	public String getVersion() {
		return version;
	}

	public UrlLinks getUrlLinks() {
		return urlLinks;
	}

	public WhatIsMyIP getWhatIsMyIP() {
		return whatIsMyIP;
	}

	public ConfigInterface getConfig() {
		return config;
	}

	public LogInterface getLog() {
		return log;
	}

	public GuiInterface getGui() {
		return gui;
	}

}
